<template>
  <div class="class-wrap">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-fa el-icon-fa fa-group"></i> 班级管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="container">
      <div class="query-form">
        <el-row :gutter="20">
          <el-col :span="2">
            <el-button @click="addClass" icon="el-icon-plus">创建</el-button>
          </el-col>
          <el-col :offset="16" :span="3">
            <el-input
              @keyup.enter.native="query"
              placeholder="班级名"
              v-model="queryForm.ClassName"
            />
          </el-col>
          <el-col :span="3">
            <el-button
              @click="getPageByKeywordIndex"
              icon="el-icon-search"
              type="primary"
              >搜索
            </el-button>
          </el-col>
        </el-row>
      </div>

      <el-row justify="center" type="flex">
        <el-pagination
          :current-page.sync="pageIndex"
          :page-size="pageSize"
          :total="pageSize * pageCount"
          @current-change="getPage"
          background
          layout="prev, pager, next"
        >
        </el-pagination>
      </el-row>

      <div class="table">
        <el-table :data="tableData" stripe>
          <el-table-column label="班级Id" prop="classId" />
          <el-table-column label="班级名" prop="className" />
          <el-table-column label="教室" prop="room" />
          <el-table-column label="班级人数" prop="count3" />
          <el-table-column align="center" label="操作" width="200px">
            <template slot-scope="scope">
              <el-button
                @click="edit(scope.row.classId)"
                size="mini"
                type="success"
                >编辑
              </el-button>
              <el-button @click="!isDisabled" size="mini" type="danger"
                >禁止删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-dialog :visible.sync="editing" title="编辑" width="30%">
        <el-form :model="entityForm" label-width="70px" ref="form">
          <el-form-item label="班级名">
            <el-input v-model="entityForm.className"></el-input>
          </el-form-item>
          <el-form-item label="教室">
            <el-input v-model="entityForm.room"></el-input>
          </el-form-item>
        </el-form>
        <span class="dialog-footer" slot="footer">
          <el-button @click="save" type="primary">确 定</el-button>
          <el-button @click="editing = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import * as api from "../../api/admin/class";

export default {
  name: "AdminClass",
  data() {
    return {
      queryForm: {},
      entityForm: {},
      tableData: [],
      pageSize: api.pageSize,
      pageCount: 1,
      pageIndex: 1,
      editing: false,
      majors: []
    };
  },
  methods: {
    getPageByKeywordIndex() {
      api.getPageCount().then(res => {
        this.pageCount = res;
        this.pageIndex = 1;
        this.getPage(1);
      });
    },
    getPage(pageIndex) {
      // eslint-disable-next-line prettier/prettier
      api
        .getPage(
          pageIndex
        )
        .then(res => {
        this.tableData = res;
      });
    },
    addClass() {
      this.entityForm = {
        classId: -1,
        className: "",
        room: "" // 将"room"初始化为一个初始值，可以是空字符串或你喜欢的任何值。
      };
      this.editing = true;
    },

    edit(classId) {
      api.get(classId).then(res => {
        this.entityForm = res;
        this.editing = true;
      });
    },
    save() {
      if (this.entityForm.classId === -1) {
        api.addClass(this.entityForm).then(() => {
          this.finishSave();
        });
      } else {
        api.update(this.entityForm).then(() => {
          this.finishSave();
        });
      }
    },
    finishSave() {
      this.$message.success("成功");
      this.getPage(this.pageIndex);
      this.editing = false;
    },
    deleteItem(classId) {
      api.deleteItem(classId).then(() => {
        this.$message.success("删除成功");
        this.getPage(this.pageIndex);
      });
    }
  },
  created() {
    this.getPageByKeywordIndex();
  }
};
</script>

<style scoped></style>
